package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.User;
import com.karson.common.request.PageParamRequest;
import com.karson.common.request.UserSearchRequest;
import com.karson.common.request.UserStatusRequest;
import com.karson.common.response.CommonResult;
import com.karson.common.response.UserResponse;
import com.karson.service.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<UserResponse>> getList(@ModelAttribute @Validated @ParameterObject UserSearchRequest request,
                                @ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {

        IPage<UserResponse> userResponsePage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<UserResponse> userCommonPage =  userService.pageList(userResponsePage,request);
        return CommonResult.success(userCommonPage);
    }
    @Operation(summary = "查看个人中心")
    @GetMapping("/info/{uid}")
    public CommonResult<UserResponse> getInfo(@PathVariable("uid") Integer uid) {
       UserResponse userResponse =  userService.getInfo(uid);
       return CommonResult.success(userResponse);
    }
    @Operation(summary = "注销用户")
    @DeleteMapping("/del/{uid}")
    public CommonResult<Void> del(@PathVariable("uid") Integer uid) {

        boolean flag = userService.delUser(uid);
        if (flag) {
            return CommonResult.success("删除成功");
        }else{
            return CommonResult.failed("删除失败");
        }
    }

    @Operation(summary = "更新状态")
    @GetMapping("/updateStatus")
    public CommonResult<Void> updateStatus(@RequestParam Integer uid,
                                           @RequestParam Boolean status) {

        boolean flag = userService.updateStatus(uid,status);
        if (flag) {
            return CommonResult.success("更新成功");
        }else{
            return CommonResult.failed("更新失败");
        }
    }
}
